/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rps.controller;

import bean.entity.Employees;
import bean.entity.Interviews;
import bean.session.EmployeeSessionBean;
import bean.session.InterviewSessionBean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIInput;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

/**
 *
 * @author Vu Thang
 */
@ManagedBean
@SessionScoped
public class EmployeeManagedBean implements Serializable {

    @EJB
    private InterviewSessionBean interviewSessionBean;
    @EJB
    private EmployeeSessionBean employeeSessionBean;
    private Employees emp;
    private String error;
    private String OldPass;
    private String newPass;
    private String confirmpass;
    private List<Interviews> listInterviews;

    /** Creates a new instance of EmployeeManagedBean */
    public EmployeeManagedBean() {
        emp = new Employees();
        emp.setEmpNo(null);
        listInterviews = new LinkedList<Interviews>();

    }

    public Employees getEmp() {
        return emp;
    }

    public void setEmp(Employees emp) {
        this.emp = emp;
    }

    public void changeProfileEm() {
        employeeSessionBean.updateEmployes(emp);
    }

    public void changePassword() {
        String id = emp.getEmpNo();
        employeeSessionBean.changePassEmployes(id, confirmpass);
        this.newPass = "";
        this.confirmpass = "";
        this.OldPass = "";

    }

    public String verify_emp() {
        if (employeeSessionBean.checkLogin(emp.getEmpNo(), emp.getPassword()) != null) {
            emp = employeeSessionBean.checkLogin(emp.getEmpNo(), emp.getPassword());
            if (emp.getDepartments().getDepartmentID().equals("D0001")) {
                return "pretty:hrgroup";
            } else {
                return "pretty:interviewer";
            }

        } else {
            emp.setEmpNo(null);
            error = "Invalid Employee No or Password";
            return "pretty:emlogin";
        }
    }

    public int getCount() {
        int count1 = interviewSessionBean.countInterviews(emp.getEmpNo());
        listInterviews = interviewSessionBean.findAllInterviewses(emp.getEmpNo());
        return count1;
    }

    public String Logout() {
        emp = null;
        emp = new Employees();
        error = "";
        return "pretty:emlogin";
    }

    public boolean isLogouted() {
        return emp.getEmpNo() != null;
    }

    public boolean isLogin() {
        return emp.getEmpNo() == null;
    }

    public String checkVerify() {

        if (emp.getEmpNo() != null) {
            if (emp.getDepartments().getDepartmentID().equals("D0001")) {
                return "pretty:hrgroup";
            } else {
                return "pretty:interviewer";
            }
        } else {
            error = "";
            return "pretty:emlogin";
        }
    }

    public void validateForm(ActionEvent e) {
        UIInput ui = (UIInput) e.getComponent();
        String check = (String) ui.getValue();
        if (check.equals("")) {
            error = "(*)";
        }
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    /**
     * @return the OldPass
     */
    public String getOldPass() {
        return OldPass;
    }

    /**
     * @param OldPass the OldPass to set
     */
    public void setOldPass(String OldPass) {
        this.OldPass = OldPass;
    }

    /**
     * @return the newPass
     */
    public String getNewPass() {
        return newPass;
    }

    /**
     * @param newPass the newPass to set
     */
    public void setNewPass(String newPass) {
        this.newPass = newPass;
    }

    /**
     * @return the confirmpass
     */
    public String getConfirmpass() {
        return confirmpass;
    }

    /**
     * @param confirmpass the confirmpass to set
     */
    public void setConfirmpass(String confirmpass) {
        this.confirmpass = confirmpass;
    }

    /**
     * @return the listInterviews
     */
    public List<Interviews> getListInterviews() {
        return listInterviews;
    }

    /**
     * @param listInterviews the listInterviews to set
     */
    public void setListInterviews(List<Interviews> listInterviews) {
        this.listInterviews = listInterviews;
    }


    public void filterSchedule(ValueChangeEvent e)
    {
        UIInput in = (UIInput) e.getComponent();
        String filter = (String) in.getValue();
        List<Interviews> lstIn = new ArrayList<Interviews>();
        Calendar today = Calendar.getInstance();
        lstIn=interviewSessionBean.findAllInterviewses(emp.getEmpNo());
        if(filter.equals("day"))
        {
            //listInterviews= interviewSessionBean.filterAllInterviewses(emp.getEmpNo(),today);
            for(Interviews item : lstIn)
            {
                if(today.DAY_OF_MONTH==item.getScheduleDate().getDay()){
                    listInterviews.add(item);                    
                }
                me=today.DAY_OF_MONTH;
            }
            
        }else if(filter.equals("month"))
        {
            //listInterviews = interviewSessionBean.filterAllInterviewses(emp.getEmpNo(),today);
            for(Interviews item : lstIn)
            {
                if(today.MONTH==item.getScheduleDate().getMonth()){
                    listInterviews.add(item);
                }
                me=today.MONTH;
            }
        }else{
            for(Interviews item : lstIn)
            {
                if(today.YEAR==item.getScheduleDate().getYear()){
                    listInterviews.add(item);
                }
                
            }
        }
    }
    int me;

    public int getMe() {
        return me;
    }

    public void setMe(int me) {
        this.me = me;
    }
}
